// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.properties;

import java.util.Map;

import org.dengues.core.components.ENodeParameterName;
import org.dengues.core.warehouse.IWarehouseObject;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.warehouse.ProcessType;
import org.dengues.ui.PropertiesUiUtils;
import org.dengues.warehouse.i18n.Messages;
import org.dengues.warehouse.models.WarehouseNode;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class DescriptionSection extends AbstractPropertySection {

    private Composite composite;

    private TabbedPropertySheetWidgetFactory widgetFactory;

    /**
     * Qiang.Zhang.Adolf@gmail.com DescriptionSection constructor comment.
     */
    public DescriptionSection() {
        super();
    }

    public DescriptionSection(TabbedPropertySheetWidgetFactory widgetFactory) {
        super();
        this.widgetFactory = widgetFactory;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#setInput(org.eclipse.ui.IWorkbenchPart,
     * org.eclipse.jface.viewers.ISelection)
     */
    @Override
    public void setInput(IWorkbenchPart part, ISelection selection) {
        Object firstElement = ((IStructuredSelection) selection).getFirstElement();
        WarehouseNode warehouseNode = SectionFilter.getWarehouseNode(firstElement);
        if (warehouseNode == null) {
            return;
        }
        selection = new StructuredSelection(warehouseNode);
        super.setInput(part, selection);
        addComposite();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite,
     * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
     */
    @Override
    public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
        super.createControls(parent, tabbedPropertySheetPage);
        composite = parent;
        GridLayout gridLayout = new GridLayout();
        gridLayout.marginBottom = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginWidth = 0;
        composite.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_BOTH);
        composite.setLayoutData(gridData);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "addComposite".
     */
    private void addComposite() {
        if (composite != null) {
            Control[] children = composite.getChildren();
            for (Control control : children) {
                control.dispose();
            }
        }
        GridLayout gridLayout = new GridLayout(2, false);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);

        IStructuredSelection select = (IStructuredSelection) getSelection();
        Object firstElement = select.getFirstElement();
        if (firstElement instanceof WarehouseNode) {
            WarehouseNode node = (WarehouseNode) firstElement;
            gridData = addNameSection(gridLayout, gridData, node);
            IWarehouseObject object = node.getObject();
            DatabaseDiagram data = null;
            if (object != null && object.getData() != null) {
                String des = ""; //$NON-NLS-1$
                if (object.getData() instanceof ProcessType) {
                    String comment = ((ProcessType) object.getData()).getComment();
                    des = comment != null ? comment : ""; //$NON-NLS-1$
                } else if (object.getData() instanceof DatabaseDiagram) {
                    data = (DatabaseDiagram) object.getData();
                    Object object2 = (data).getProperties().get(ENodeParameterName.DB_COMMENT.getName());
                    des = object2 != null ? object2.toString() : ""; //$NON-NLS-1$
                }
                Composite descComposite = getWidgetFactory().createComposite(composite, SWT.NONE);
                descComposite.setLayout(gridLayout);
                descComposite.setLayoutData(gridData);
                Label label = getWidgetFactory().createLabel(descComposite,
                        Messages.getString("DescriptionSection.desc"), SWT.NONE); //$NON-NLS-1$
                gridData = new GridData();
                gridData.widthHint = 80;
                label.setLayoutData(gridData);
                Text text = getWidgetFactory().createText(descComposite, des, SWT.BORDER);
                gridData = new GridData(GridData.FILL_HORIZONTAL);
                text.setLayoutData(gridData);
                gridData.heightHint = text.getLineHeight() * 4;
                text.setEnabled(false);
                if (data != null) {
                    Map properties = data.getProperties();
                    PropertiesUiUtils.createPropertiesControls(getWidgetFactory(), descComposite, properties);
                }
            }
        }

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "addNameSection".
     * 
     * @param gridLayout
     * @param gridData
     * @param node
     * @return
     */
    private GridData addNameSection(GridLayout gridLayout, GridData gridData, WarehouseNode node) {
        Composite nameComposite = getWidgetFactory().createComposite(composite, SWT.NONE);
        nameComposite.setLayout(gridLayout);
        nameComposite.setLayoutData(gridData);
        Label label = getWidgetFactory().createLabel(nameComposite, Messages.getString("NameSection.name"), SWT.NONE); //$NON-NLS-1$
        gridData = new GridData();
        gridData.widthHint = 80;
        label.setLayoutData(gridData);
        Text text = getWidgetFactory().createText(nameComposite, node.getLabel(), SWT.NONE);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        text.setLayoutData(gridData);
        text.setEnabled(false);

        label = getWidgetFactory().createLabel(nameComposite, Messages.getString("NameSection.category"), SWT.NONE); //$NON-NLS-1$
        gridData = new GridData();

        label.setLayoutData(gridData);
        text = getWidgetFactory().createText(nameComposite, node.getCategoryName().getDisplayName(), SWT.BORDER);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        text.setLayoutData(gridData);
        text.setEnabled(false);
        return gridData;
    }

    @Override
    public void refresh() {
        composite.getParent().layout(true, true);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#getWidgetFactory()
     */
    @Override
    public TabbedPropertySheetWidgetFactory getWidgetFactory() {
        if (widgetFactory != null) {
            return widgetFactory;
        }
        return super.getWidgetFactory();
    }

}
